<?php
/**
 * 用户管理
 *
 * @author     chenfenghua<843958575@qq.com>
 * @copyright  Copyright 2014-2016
 * @version    2.0
 */
namespace app\backend\modules\admin\controllers;

use app\backend\modules\admin\models\AppRole;
use app\models\admin\models\AdminUser;
use app\models\base\LoginLog;
use Yii;
use app\backend\components\BaseController;

class UserController extends BaseController
{
    public $layout = '@app/backend/views/layouts/main_login';

    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 用户登录
     */
    public function actionLogin()
    {
        $this->data['js'] = array(
            /*begin validate*/
            'global/plugins/jquery-validation/js/jquery.validate.min.js',
            'global/plugins/jquery-validation/js/additional-methods.min.js',
            'global/plugins/select2/js/select2.full.min.js',
            /*end validate*/
            'pages/scripts/login.js'
        );
        $this->data['css'] = array(
            'pages/css/login.min.css'
        );
        $this->data['error'] = '';
        if (Yii::$app->request->post()) {
            $userParam = Yii::$app->request->post('User');
            $result = AdminUser::find()->where([
                'login_account' => $userParam['username'],
                'password' => md5(md5($userParam['password'])),
                'status' => 0,
            ])->one();
            if (!$result) $this->data['error'] = '用户名或密码错误';
            else {
                //保存用户信息
                $this->addSession($result);
                //var_dump($result);exit;
                $this->redirect('?r=admin/default/index');
                //更改管理员登录信息
                AdminUser::updateAll(
                    [
                        'last_login_time' => time(),
                        'last_login_ip' => Yii::$app->request->userIP,
                    ],
                    'user_id = :user_id',
                    [':user_id' => $result['user_id']]
                );
                //记录登录日志
                $loginModel = new LoginLog();
                $loginModel->identity = 0;
                $loginModel->identity_id = $result['user_id'];
                $loginModel->create_time = time();
                $loginModel->save();
            }
        }
        return $this->render('login', $this->data);
    }

    /**
     * 退出
     */
    public function actionLogout()
    {
        Yii::$app->session->remove('admin_user_id');
        Yii::$app->session->remove('login_account');
        Yii::$app->session->remove('super');

        $this->redirect('?r=admin/user/login');
    }

    /**
     * 注入session
     *
     * @param $memberParams
     */
    protected function addSession($memberParams)
    {
        //用户权限
        Yii::$app->session['admin_user_id'] = $memberParams['user_id'];
        Yii::$app->session['login_account'] = $memberParams['login_account'];
        #判断是否超级管理员
        if ($memberParams['login_account'] == 'admin') Yii::$app->session['super'] = 1;
        else {
            Yii::$app->session['super'] = 0;
            $appRole = new AppRole();
            $role_row = $appRole->getRoleById($memberParams['user_id']);

            Yii::$app->session['group_name'] = $role_row['group_name'];
            Yii::$app->session['acl'] = $role_row['acl'];
        }
    }
}
